//
//  UITabBarController+SwiftBox.swift
//  QSwiftBoxExample
//
//  Created by carefree on 2020/6/20.
//  Copyright © 2020 carefree. All rights reserved.
//

import UIKit

extension QSwiftBox where Base: UITabBarController {
    public func selectViewController(_ clazz: UIViewController.Type) {
        var index = -1
        let currentNav = base.selectedViewController
        
        if let list = base.viewControllers {
            for i in 0 ..< list.count {
                if let nav = list[i] as? UINavigationController, nav.viewControllers.first?.isKind(of: clazz) == true {
                    index = i
                    break
                }
            }
        }
        
        if index >= 0 {
            base.selectedIndex = index
            if let nav = currentNav as? UINavigationController, let root = nav.viewControllers.first, nav.viewControllers.count > 1 {
                nav.setViewControllers([root], animated: false)
            }
        }
    }
}
